﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SMS
{
    public partial class fQuanlytinnhan : Form
    {
           fLogin main;
        public fQuanlytinnhan (fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }
         
        public void fQuanlytinnhan_Load(object sender, EventArgs e)
        {
            main.Cauhinhtinnhan.Visible = false;
            main.Lichsutinnhan.Visible = false;
            pictureBox1.Visible = true;
            dpFrom.Enabled = false;
            dpTo.Enabled = false;
            cboKibaoduong.Enabled = false;
            cboTinhtrang.Enabled = false;
            bntTim.Enabled = false;
        }

        private void tsmiBill_Click(object sender, EventArgs e)
        {

        }

        private void bntVeGiaoDienChinh_Click_1(object sender, EventArgs e)
        {
            this.Visible = false;
            main.HeThong.Visible = true;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.Visible = true;
        }

        private void lnkDoiMatKau_Click(object sender, EventArgs e)
        {
            main.DoiMatKhau.ShowDialog();
        }

        private void bntThietLapCauHinhTinNhan_Click_1(object sender, EventArgs e)
        {
            panel1.Controls.Add(main.Cauhinhtinnhan);
            main.Cauhinhtinnhan.Show();
            main.Cauhinhtinnhan.BringToFront();
            pictureBox1.Visible = false;
            dpFrom.Enabled = false;
            dpTo.Enabled = false;
            cboKibaoduong.Enabled = false;
            cboTinhtrang.Enabled = false;
            bntTim.Enabled = false;
        }

        private void bntXemLichSuTinNhan_Click_1(object sender, EventArgs e)
        {
            panel1.Controls.Add(main.Lichsutinnhan);
            main.Lichsutinnhan.Show();
            main.Lichsutinnhan.BringToFront();
            pictureBox1.Visible = false;
            dpFrom.Enabled = true;
            dpTo.Enabled = true;
            cboKibaoduong.Enabled = true;
            cboKibaoduong.SelectedIndex = 0;
            cboTinhtrang.Enabled = true;
            cboTinhtrang.SelectedIndex = 0;
            bntTim.Enabled = true;
            dpFrom.Value = new DateTime(2011,12,10);
        }

        private void tsmiQuanLyKhachHang_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.QuanLyKhachHang.Visible = true;
            main.QuanLyKhachHang.fQuanlykhachhang_Load(null, null);
        }

        private void tsmiQuanLyXe_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.QuanLyXe.Visible = true;
            main.QuanLyXe.fQuanlyxe_Load(null, null);
        }

        private void ttQuanlitaikhoan_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.QuanLyTaiKhoan.Visible = true;
            main.QuanLyTaiKhoan.FQuanlytaikhoan_Load_1(null, null);
        }

        private void picExit_Click(object sender, EventArgs e)
        {
            this.Visible = false;
            main.Visible = true;
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc");
        }

        private void bntTim_Click(object sender, EventArgs e)
        {
            if (cboKibaoduong.Text.ToString() == "Tất cả" && cboTinhtrang.Text.ToString() == "Tất cả" )
            {
                DataTable dtbResult = new DataTable();

                SqlConnection conn = new SqlConnection(main.ConnectDB);
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = "SELECT HOTEN AS N'TÊN KHÁCH HÀNG', KH.CMND, "
                + "DTDIDONG AS N'SỐ ĐIỆN THOẠI', NOIDUNG AS N'NỘI DUNG TIN NHẮN', SOKHUNG N'SỐ KHUNG',  "
                + "LOAITN AS N'LOẠI TIN NHẮN', THOIGIANGUI AS N'GỬI LÚC', NK.TINHTRANG AS N'TÌNH TRẠNG GỬI'  "
                + "FROM NHATKITINNHAN NK, KHACHHANG KH WHERE NK.CMND = KH.CMND AND THOIGIANGUI >= @FROM AND THOIGIANGUI <= @TO ";

                command.Parameters.Add(new SqlParameter("@FROM", dpFrom.Value.ToString()));
                command.Parameters.Add(new SqlParameter("@TO", dpTo.Value.ToString()));


                SqlDataAdapter adapter = new SqlDataAdapter(command);
                try
                {
                    adapter.Fill(dtbResult);
                    main.Lichsutinnhan.dataGridView1.DataSource = dtbResult;

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    command.Dispose();
                    conn.Close();
                }
            }

            if (cboKibaoduong.Text.ToString() != "Tất cả" && cboTinhtrang.Text.ToString() == "Tất cả" )
            {
                DataTable dtbResult = new DataTable();

                SqlConnection conn = new SqlConnection(main.ConnectDB);
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = "SELECT HOTEN AS N'TÊN KHÁCH HÀNG', KH.CMND, "
                + "DTDIDONG AS N'SỐ ĐIỆN THOẠI', NOIDUNG AS N'NỘI DUNG TIN NHẮN', SOKHUNG N'SỐ KHUNG',  "
                + "LOAITN AS N'LOẠI TIN NHẮN', THOIGIANGUI AS N'GỬI LÚC', NK.TINHTRANG AS N'TÌNH TRẠNG GỬI'  "
                + "FROM NHATKITINNHAN NK, KHACHHANG KH WHERE NK.CMND = KH.CMND AND THOIGIANGUI >= @FROM AND THOIGIANGUI <= @TO AND LOAITN = @LOAITN ";

                command.Parameters.Add(new SqlParameter("@FROM", dpFrom.Value.ToString()));
                command.Parameters.Add(new SqlParameter("@TO", dpTo.Value.ToString()));
                command.Parameters.Add(new SqlParameter("@LOAITN", cboKibaoduong.Text.ToString()));


                SqlDataAdapter adapter = new SqlDataAdapter(command);
                try
                {
                    adapter.Fill(dtbResult);
                    main.Lichsutinnhan.dataGridView1.DataSource = dtbResult;

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    command.Dispose();
                    conn.Close();
                }
            }

            if (cboKibaoduong.Text.ToString() != "Tất cả" && cboTinhtrang.Text.ToString() != "Tất cả")
            {
                DataTable dtbResult = new DataTable();

                SqlConnection conn = new SqlConnection(main.ConnectDB);
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = "SELECT HOTEN AS N'TÊN KHÁCH HÀNG', KH.CMND, "
                + "DTDIDONG AS N'SỐ ĐIỆN THOẠI', NOIDUNG AS N'NỘI DUNG TIN NHẮN', SOKHUNG N'SỐ KHUNG',  "
                + "LOAITN AS N'LOẠI TIN NHẮN', THOIGIANGUI AS N'GỬI LÚC', NK.TINHTRANG AS N'TÌNH TRẠNG GỬI'  "
                + "FROM NHATKITINNHAN NK, KHACHHANG KH WHERE NK.CMND = KH.CMND AND THOIGIANGUI >= @FROM AND THOIGIANGUI <= @TO AND LOAITN = @LOAITN AND NK.TINHTRANG = @TINHTRANG";

                command.Parameters.Add(new SqlParameter("@FROM", dpFrom.Value.ToString()));
                command.Parameters.Add(new SqlParameter("@TO", dpTo.Value.ToString()));
                command.Parameters.Add(new SqlParameter("@LOAITN", cboKibaoduong.Text.ToString()));
                command.Parameters.Add(new SqlParameter("@TINHTRANG", cboTinhtrang.Text.ToString()));


                SqlDataAdapter adapter = new SqlDataAdapter(command);
                try
                {
                    adapter.Fill(dtbResult);
                    main.Lichsutinnhan.dataGridView1.DataSource = dtbResult;

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Khong thanh cong");
                }
                finally
                {
                    command.Dispose();
                    conn.Close();
                }
            }

            if (cboKibaoduong.Text.ToString() == "Tất cả" && cboTinhtrang.Text.ToString() != "Tất cả")
            {
                DataTable dtbResult = new DataTable();

                SqlConnection conn = new SqlConnection(main.ConnectDB);
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandText = "SELECT HOTEN AS N'TÊN KHÁCH HÀNG', KH.CMND, "
                + "DTDIDONG AS N'SỐ ĐIỆN THOẠI', NOIDUNG AS N'NỘI DUNG TIN NHẮN', SOKHUNG N'SỐ KHUNG',  "
                + "LOAITN AS N'LOẠI TIN NHẮN', THOIGIANGUI AS N'GỬI LÚC', NK.TINHTRANG AS N'TÌNH TRẠNG GỬI'  "
                + "FROM NHATKITINNHAN NK, KHACHHANG KH WHERE NK.CMND = KH.CMND AND THOIGIANGUI >= @FROM AND THOIGIANGUI <= @TO AND NK.TINHTRANG = @TINHTRANG";

                command.Parameters.Add(new SqlParameter("@FROM", dpFrom.Value.ToShortDateString()));
                command.Parameters.Add(new SqlParameter("@TO", dpTo.Value.ToShortDateString()));
                command.Parameters.Add(new SqlParameter("@TINHTRANG", cboTinhtrang.Text.ToString()));


                SqlDataAdapter adapter = new SqlDataAdapter(command);
                try
                {
                    adapter.Fill(dtbResult);
                    main.Lichsutinnhan.dataGridView1.DataSource = dtbResult;

                }
                catch (Exception ex)
                {
                    string err = ex.Message;
                }
                finally
                {
                    command.Dispose();
                    conn.Close();
                }
            }



            
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            try
            {
                //Process k = new Process();
                //k.StartInfo.FileName = "C:\\Users\\PHUNGUYEN\\Desktop\\Year 3\\PM\\New folder\\SMS\\SMS\\PoD.doc";
                //k.StartInfo.Arguments = "";
                //k.Start();
                System.Diagnostics.Process prc = new System.Diagnostics.Process();
                string currentDir = Environment.CurrentDirectory;//lấy đường dẫn thư mục hiện tại.
                prc.StartInfo.FileName = currentDir + "\\PoD\\User Guide SMS_SYSTEM.pdf";
                prc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể mở file!");
            }
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            try
            {
                System.Diagnostics.Process prc = new System.Diagnostics.Process();
                string currentDir = Environment.CurrentDirectory;//lấy đường dẫn thư mục hiện tại.
                prc.StartInfo.FileName = currentDir + "\\PoD\\PoD.html";
                prc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể mở file!");
            }
        }



        

       

       
    }
}
